# Using the implemented model utils to forecast the absolute view vector and se.
source("ARIMA.R") # ARIMA model

forecast.arima <- function(ff.train, n.ahead=1) {
	# Forecast the absolute view on the (next period) returns for all 6 returns using ARIMA models.
	# Args:
	#   ff.train: the FF6 data in the training window.
	# Returns:
	#   Q: the absolute view N*1 column vector of the forecasted returns. 
	
	#smlo
	ts.smlo <- xts(ff.train$smlo_vwret, as.Date(as.character(ff.train$date), format="%Y%m%d"))
	model.smlo <- arma.model.selection(ts.smlo, 3, 3)
	p.smlo <- model.smlo[["p"]]
	q.smlo <- model.smlo[["q"]]
	#print(unlist(model.smlo))
	fit.smlo <- arima(ts.smlo, order=c(p.smlo, 0, q.smlo), method="ML")	
	predict.smlo <- predict(fit.smlo, n.ahead)
	smlo.pred <- predict.smlo$pred[1]
	smlo.se <- predict.smlo$se[1]
	
	#smme
	ts.smme <- xts(ff.train$smme_vwret, as.Date(as.character(ff.train$date), format="%Y%m%d"))
	model.smme <- arma.model.selection(ts.smme, 3, 3)
	p.smme <- model.smme[["p"]]
	q.smme <- model.smme[["q"]]
	#print(unlist(model.smme))
	fit.smme <- arima(ts.smme, order=c(p.smme, 0, q.smme), method="ML")	
	predict.smme <- predict(fit.smme, n.ahead)
	smme.pred <- predict.smme$pred[1]
	smme.se <- predict.smme$se[1]
	
	#smhi
	ts.smhi <- xts(ff.train$smhi_vwret, as.Date(as.character(ff.train$date), format="%Y%m%d"))
	model.smhi <- arma.model.selection(ts.smhi, 3, 3)
	p.smhi <- model.smhi[["p"]]
	q.smhi <- model.smhi[["q"]]
	#print(unlist(model.smhi))
	fit.smhi <- arima(ts.smhi, order=c(p.smhi, 0, q.smhi), method="ML")	
	predict.smhi <- predict(fit.smhi, n.ahead)
	smhi.pred <- predict.smhi$pred[1]
	smhi.se <- predict.smhi$se[1]
	
	#bilo
	ts.bilo <- xts(ff.train$bilo_vwret, as.Date(as.character(ff.train$date), format="%Y%m%d"))
	model.bilo <- arma.model.selection(ts.bilo, 3, 3)
	p.bilo <- model.bilo[["p"]]
	q.bilo <- model.bilo[["q"]]
	#print(unlist(model.bilo))
	fit.bilo <- arima(ts.bilo, order=c(p.bilo, 0, q.bilo), method="ML")	
	predict.bilo <- predict(fit.bilo, n.ahead)
	bilo.pred <- predict.bilo$pred[1]
	bilo.se <- predict.bilo$se[1]
	
	#bime
	ts.bime <- xts(ff.train$bime_vwret, as.Date(as.character(ff.train$date), format="%Y%m%d"))
	model.bime <- arma.model.selection(ts.bime, 3, 3)
	p.bime <- model.bime[["p"]]
	q.bime <- model.bime[["q"]]
	#print(unlist(model.bime))
	fit.bime <- arima(ts.bime, order=c(p.bime, 0, q.bime), method="ML")	
	predict.bime <- predict(fit.bime, n.ahead)
	bime.pred <- predict.bime$pred[1]
	bime.se <- predict.bime$se[1]
	
	#bihi
	ts.bihi <- xts(ff.train$bihi_vwret, as.Date(as.character(ff.train$date), format="%Y%m%d"))
	model.bihi <- arma.model.selection(ts.bihi, 3, 3)
	p.bihi <- model.bihi[["p"]]
	q.bihi <- model.bihi[["q"]]
	#print(unlist(model.bihi))
	fit.bihi <- arima(ts.bihi, order=c(p.bihi, 0, q.bihi), method="ML")	
	predict.bihi <- predict(fit.bihi, n.ahead)
	bihi.pred <- predict.bihi$pred[1]
	bihi.se <- predict.bihi$se[1]
	
	return(list(Q=c(smlo.pred, smme.pred, smhi.pred, bilo.pred, bime.pred, bihi.pred),
				SE=c(smlo.se, smme.se, smhi.se, bilo.se, bime.se, bihi.se)))
}
